﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using COMOSOFT.WareHouse_Management.Report;
using DevExpress.XtraReports.UI;
using DevExpress.LookAndFeel;

namespace COMOSOFT.WareHouse_Management.Forms
{
    public partial class FrmExportMA : Form
    {
        ClassShare cls = new ClassShare("FrmExportMA");
        int MAEID,MAEDID,MAID,MADID,MALDID;
        double ExptQty;
        string Remark=null;
        List<object> ListItem = new List<object>();
        string items;
        public FrmExportMA(int _MAEID)
        {
            InitializeComponent();
            MAEID = _MAEID;
        }

        #region[LoadData]
        public void LoadData()
        {
            try
            {
                factoryListTableAdapter.Fill(dataSet_WareHouse.FactoryList);
                employeeTableAdapter.Fill(dataSet_WareHouse.Employee);
                if (MAEID > 0)
                {
                    mAExportMasterTableAdapter.Fill(dataSet_WareHouse.MAExportMaster, MAEID);
                    sp_ExportMADetailsTableAdapter.Fill(dataSet_WareHouse.sp_ExportMADetails, MAEID);                    
                    sp_WHExptToPOTableAdapter.Fill(this.dataSet_WareHouse.sp_WHExptToPO);
                    this.Text = txtExportNo.Text + '-' + txtFactory.Text;
                    MAEID = Convert.ToInt32(txtMAEID.EditValue);
                    if (groupControl_Details.Enabled == false)
                        groupControl_Details.Enabled = true;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[AddNew]
        public void AddNew()
        {
            mAExportMasterBindingSource.AddNew();
            txtIssuaDate.EditValue = DateTime.Now.ToShortDateString();
            txtRequester.EditValue = Convert.ToInt64(ClassShare.Usid);
        }
        #endregion

        #region[UpdateData Master]
        public void UpdateData(string com)
        {
            try
            {
                if (com == "Update")
                {
                    mAExportMasterBindingSource.EndEdit();
                    mAExportMasterTableAdapter.Update(dataSet_WareHouse.MAExportMaster);
                    dataSet_WareHouse.MAExportMaster.AcceptChanges();
                    MAEID = Convert.ToInt32(txtMAEID.EditValue);
                }
                else
                {
                    if (MAEID > 0)
                    {
                        if (MessageBox.Show("Are you sure delete?", "Comfirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            dataSet_WareHouse.MAExportMaster.Rows[0].Delete();
                            mAExportMasterTableAdapter.Update(dataSet_WareHouse.MAExportMaster);
                            dataSet_WareHouse.MAExportMaster.AcceptChanges();
                        }
                    }
                }
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[ChooseMA]   
        private void repositoryChooseMA_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            dockPanel_ChooseMA.Show();
            sp_WHExportChooseMATableAdapter.Fill(dataSet_WareHouse.sp_WHExportChooseMA);
        }
        private void dataNavigator_ChooseMA_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            try
            {
                if (e.Button.Tag.ToString() == "Add")
                {
                    GridView view = gridView_ChooseMA as GridView;
                    int[] arrMA = view.GetSelectedRows();
                    foreach (int index in arrMA)
                    {
                        MAEID = Convert.ToInt32(txtMAEID.EditValue);
                        MAID = Convert.ToInt32(view.GetDataRow(index)["MAID"]);
                        MADID = Convert.ToInt32(view.GetDataRow(index)["MADID"]);
                        ExptQty = Convert.ToDouble(view.GetDataRow(index)["IQty"]);
                        Remark = "";
                        cls.UpdateData("sp_WHExportDetailsInsert", new string[] { "@MAEID", "@MAID", "@MADID", "@ExptQty", "@Remark" }, new object[] { MAEID, MAID, MADID, ExptQty, Remark });
                    }
                    dockPanel_ChooseMA.Close();
                    LoadData();
                }
                else
                    sp_WHExportChooseMATableAdapter.Fill(dataSet_WareHouse.sp_WHExportChooseMA);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Update ExportDetails]
        private void deleteItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {                
                if (MessageBox.Show("Delete Item", "Are you sure delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    cls.UpdateData("sp_WHExportDetailsDelete", new string[] { "@MAEDID" }, new object[] {Convert.ToInt32(gridView_ExportMA.GetFocusedDataRow()["MAEDID"])});
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView_ExportMA_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                cls.UpdateData("sp_WHExportMADetailsUpdate", new string[] { "@MAEDID", "@Qty" }, new object[] { Convert.ToInt32(gridView_ExportMA.GetFocusedDataRow()["MAEDID"]), Convert.ToDouble(gridView_ExportMA.GetFocusedDataRow()["ExptQty"]) });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Choose TrimList]
        private void checkedComboBoxEdit_ChooseTrimList_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            if(e.CloseMode.ToString()=="Normal")
            {
                items = null;
                ListItem.Clear();
                ListItem = checkedComboBoxEdit_ChooseTrimList.Properties.Items.GetCheckedValues();
                if (ListItem.Count > 0)
                {
                    foreach (object it in ListItem)
                    {
                        items += it.ToString() + ",";
                    }
                    MADID=Convert.ToInt32(gridView_ExportMA.GetFocusedDataRow()["MADID"]);
                    sp_WHExportChoosePOTableAdapter.Fill(dataSet_WareHouse.sp_WHExportChoosePO, MADID, items);
                    gridView_POList.ClearSelection();
                    dockPanel_POAllocate.Show();
                }
                else
                    MessageBox.Show("TrimList is empty!");
            }
        }
        #endregion
        
        #region[Add PO]
        private void dataNavigator_POAllocate_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            switch(e.Button.Tag.ToString())
            {
                case "Add":
                    try
                    {
                        GridView view=gridView_POList as GridView;
                        int[] arrPO = view.GetSelectedRows();
                        double TotalQty=0,ExQty;
                        ExQty = Convert.ToInt32(dataSet_WareHouse.sp_WHExportMA.Rows[0]["ExptQty"]);
                        foreach (int item in arrPO)
                        {
                            TotalQty+=ExptQty = Convert.ToDouble(view.GetDataRow(item)["NeedQty"]);                            
                        }
                        if (ExQty >= TotalQty)
                        {
                            foreach (int item in arrPO)
                            {
                                MAEDID = Convert.ToInt32(gridView_ExportMA.GetFocusedDataRow()["MAEDID"]);
                                MALDID = Convert.ToInt32(view.GetDataRow(item)["MALDID"]);
                                ExptQty = Convert.ToDouble(view.GetDataRow(item)["NeedQty"]);
                                cls.UpdateData("sp_WHExportMAAllocateInsert", new string[] { "@MALDID", "@MAEDID", "@ExptQty", "@Remark" }, new object[] { MALDID, MAEDID, ExptQty, Remark });
                                dockPanel_POAllocate.Close();
                                checkedComboBoxEdit_ChooseTrimList.EditValue = null;
                                sp_WHExportChoosePOTableAdapter.Fill(dataSet_WareHouse.sp_WHExportChoosePO, null, null);
                                LoadData();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Oder quantity is limited");
                        }
                      
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    break;
                case "Refresh":
                    sp_BAMenuTableAdapter.Fill(this.dataSetBalancing.sp_BAMenu);
                    checkedComboBoxEdit_ChooseTrimList.EditValue = null;
                    sp_WHExportChoosePOTableAdapter.Fill(dataSet_WareHouse.sp_WHExportChoosePO, null, null);
                    break;
                case "Exit":
                    dockPanel_POAllocate.Close();
                    checkedComboBoxEdit_ChooseTrimList.EditValue = null;
                    sp_WHExportChoosePOTableAdapter.Fill(dataSet_WareHouse.sp_WHExportChoosePO, null, null);
                    break;
            } 
        }
        #endregion

        #region[ShowPanel]
        private void ShowPanel_ChoosePO_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            dockPanel_POAllocate.Show();
            sp_BAMenuTableAdapter.Fill(this.dataSetBalancing.sp_BAMenu);
            sp_WHExportMATableAdapter.Fill(dataSet_WareHouse.sp_WHExportMA,Convert.ToInt32(gridView_ExportMA.GetFocusedDataRow()["MAEDID"]));
        }
        #endregion        

        private void dockPanel_POAllocate_ClosedPanel(object sender, DevExpress.XtraBars.Docking.DockPanelEventArgs e)
        {
            checkedComboBoxEdit_ChooseTrimList.EditValue = null;
            sp_WHExportChoosePOTableAdapter.Fill(dataSet_WareHouse.sp_WHExportChoosePO, null, null);
        }

        public void Preview()
        {
            rptImptWareHouse rpt = new rptImptWareHouse(MAEID);
            ReportPrintTool printTool = new ReportPrintTool(rpt);
            printTool.ShowRibbonPreview(UserLookAndFeel.Default);
        }
    }
}
